<?php

namespace App\Listeners;

use Faker\Provider\DateTime;
use Illuminate\Database\Events\QueryExecuted;
use Log;

class QueryExecutedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  illuminate.query  $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        if (env('APP_ENV', 'production') == 'production'){
            //return;
        }
        if (strtolower(env('QUEUE_DRIVER')) == 'database' && strpos($event->sql,config('queue.connections.database.table'))){
            return;
        }
        $bindings = (array)$event->bindings;
        foreach ($bindings as $index => $param) {
            if ($param instanceof DateTime) {
                $bindings[$index] = $param->format('Y-m-d H:i:s');
            }
        }
        $sql = str_replace("?", "'%s'", $event->sql);
        Log::info(vsprintf($sql,$bindings));
    }
}
